﻿@using Masa.Auth.Contracts.Admin.Infrastructure.Constants
@inherits ClientSectionCompontentBase

<MRow Class="pt-4">
    <MCol Md=12>
        <MCheckbox Disabled @bind-Value="@Dto.RequireClientSecret" Label="@T("RequireClientSecret")" TValue="bool"/>
    </MCol>
    <MCol>
        <STextField @bind-Value="@Dto.ClientSecret.Value" Label="@T("Value")" />
    </MCol>
    <MCol Md="@("auto")">
        <MMenu @bind-Value="_dateMenu"
               CloseOnContentClick="false"
               Transition="scale-transition"
               OffsetY>
            <ActivatorContent>
                <STextField @bind-Value="Dto.ClientSecret.Expiration"
                    Label="@T("Expiration")" Outlined
                    PrependInnerIcon="mdi-calendar-month"
                    Readonly Style="width:140px;"
                    @attributes="context.Attrs"></STextField>
            </ActivatorContent>
            <ChildContent>
                <SDatePicker @bind-Value="Dto.ClientSecret.Expiration"
                             NoTitle Scrollable OnInput="()=>_dateMenu=false">
                </SDatePicker>
            </ChildContent>
        </MMenu>
    </MCol>
    <MCol Md=12>
        <STextField @bind-Value="@Dto.ClientSecret.Description" Label="@T("Description")" />
    </MCol>

    <MCol Md=12>
        <SButton OnClick="AddSecret" Class="border-btn" Block>@T("Add")</SButton>
    </MCol>
    <MCol Md=12>
        <MDataTable Headers="_headers" Items="Dto.ClientSecrets" TItem="ClientSecretDto" HideDefaultFooter DisablePagination Class="table-border-none">
            <HeaderColContent Context="header">
                <span class="body2">@header.Text</span>
            </HeaderColContent>
            <ItemColContent>
                @switch (context.Header.Value)
                {
                    case "Action":
                        <MButton Class="mr-4" Icon OnClick="()=>RemoveSecret(context.Item)">
                            <SIcon>mdi-close</SIcon>
                        </MButton>
                        break;
                    default:
                        <span class="subtitle">@context.Value</span>
                        break;
                }
            </ItemColContent>
        </MDataTable>
    </MCol>
</MRow>

@code {
    [Parameter]
    public ClientCredentialDto Dto { get; set; } = new();

    [Parameter]
    public EventCallback<ClientCredentialDto> DtoChanged { get; set; }

    bool _dateMenu;
    List<DataTableHeader<ClientSecretDto>> _headers { get; set; } = default!;

    protected override Task OnParametersSetAsync()
    {
        _headers = new()
        {
            new() { Text = T("Type"), Value = nameof(ClientSecretDto.Type), Sortable = false },
            new() { Text = T("Value"), Value = nameof(ClientSecretDto.Value), Sortable = false },
            new() { Text = T("Description"), Value = nameof(ClientSecretDto.Description), Sortable = false },
            new() { Text = T("Expiration"), Value = nameof(ClientSecretDto.Expiration), Sortable = false },
            new() { Text = T("Action"), Value = "Action", Sortable = false,Align = DataTableHeaderAlign.Center },
        };
        return base.OnParametersSetAsync();
    }

    private void AddSecret()
    {
        if (string.IsNullOrWhiteSpace(Dto.ClientSecret.Value) || string.IsNullOrWhiteSpace(Dto.ClientSecret.Type))
        {
            return;
        }
        Dto.ClientSecrets.Add(Dto.ClientSecret);
        Dto.ClientSecret = new();
    }

    private void RemoveSecret(ClientSecretDto model)
    {
        Dto.ClientSecrets.Remove(model);
    }
}
